﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Util;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Specialized;
using kxcxw.Data;
using kxcxw.Data.Entities;

namespace KXCXW.Web.Models
{
    public class KXCXWMembershipProvider : SqlMembershipProvider
    {
        private KXCXWDBContext db = new KXCXWDBContext();

        public override bool ValidateUser(string username, string password)
        {
            try
            {
                List<SysUserInfo> user = (from q in db.SysUserInfoes
                                          where q.UserName == username && q.UserPwd == password
                                          select q).ToList();
                if (user.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch
            {
                return false;
            }

        }

        public override MembershipUser CreateUser(string username, string password, string email, string verificationCode, string passwordAnswer,
    bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            try
            {
                SysUserInfo userinfo = new SysUserInfo();
                userinfo.VerificationCode = verificationCode;
                userinfo.UserName = username;
                userinfo.UserPwd = password;
                userinfo.ConfirmPassword = password;
                userinfo.Email = email;
                userinfo.UserType = "584745A6-F18F-4B54-B289-A7DF3DD2746C";//普通用户
                userinfo.RegistrDate = DateTime.Now;
                userinfo.UserID = Guid.NewGuid().ToString();
                userinfo.NickName = "开心粉丝";
                db.SysUserInfoes.Add(userinfo);
                db.SaveChanges();
                MembershipUser user = new MembershipUser("KXCXWMembershipProvider", username, providerUserKey, email, verificationCode, "", isApproved, true,
    DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
                status = MembershipCreateStatus.Success;
                return user;
            }
            catch
            {
                status = MembershipCreateStatus.ProviderError;
                return null;
            }

        }


        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            bool flag;
            try
            {
                SysUserInfo userinfo = (from q in db.SysUserInfoes
                                        where q.UserName == username && q.UserPwd == oldPassword
                                        select q).FirstOrDefault();
                userinfo.UserPwd = newPassword;
                userinfo.ConfirmPassword = newPassword;
                db.Entry(userinfo).State = EntityState.Modified;
                db.SaveChanges();
                flag = true;
            }
            catch
            {
                flag = false;
                throw;
            }
            return flag;
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            MembershipUser user;
            try
            {
                var UserInfo = (from q in db.SysUserInfoes
                                where q.UserName == username
                                select q).FirstOrDefault();
                if (UserInfo != null)
                {
                    DateTime creationDate = UserInfo.RegistrDate == null ? DateTime.Now : Convert.ToDateTime(UserInfo.RegistrDate);
                    DateTime lastLoginDate = Convert.ToDateTime(UserInfo.LastLoginDate);
                    Guid providerUserKey = new Guid(UserInfo.UserID);
                    string email = UserInfo.Email;
                    bool isApproved = true;
                    return new MembershipUser("KXCXWMembershipProvider", username, providerUserKey, email, "", "", isApproved, true,
                                DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
                }
                user = null;
            }
            catch
            {
                throw;
            }
            return user;
        }


    }
}